package a14_SimpleStudentSystem;

import java.io.*;
import java.util.*;

import java.util.ArrayList;

/**
 * @Author quan
 * @Description 初级学生管理系统
 * @Date 2023/1/31 17:31
 */
public class TestDemo {
    public static void main(String[] args) throws IOException {

        ArrayList<Student> list = new ArrayList<>();
        BufferedReader br = new BufferedReader(new FileReader("students.txt"));
        while (true){
            String str = br.readLine();
            if(str==null){
                break;
            }else{
                System.out.println(str.length());
                String [] strArr = str.split(",");
                String name = strArr[0];
                System.out.println(name);

                int age = Integer.parseInt(strArr[1]);
                String id = strArr[2];
                String address = strArr[3];
                Student stu = new Student(name,age,id,address);
                list.add(stu);
            }
        }

        outerloop:while (true) {
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1:添加学生");
            System.out.println("2:删除学生");
            System.out.println("3:修改学生");
            System.out.println("4:查询学生");
            System.out.println("5:退出");
            Scanner sc = new Scanner(System.in);
            System.out.println("请选择：");
            String choose = sc.next();
            switch (choose){//或者写为case"1" -> sout("添加学生");后面不用写break
                case "1":
                    add(list);
                    break;
                case "2":
                    delete(list);
                    break;
                case "3":
                    update(list);
                    break;
                case "4":
                    query(list);
                    break;
                case "5":
                    System.out.println("退出系统");

                    BufferedWriter bw = new BufferedWriter(new FileWriter("students.txt"));
                    for (Student s : list) {
                        //把学生对象的数据拼接成指定格式的字符串
                        StringBuilder sb = new StringBuilder();
                        sb.append(s.getName()).append(",").append(s.getId()).append(",").append(s.getAge()).append(",").append(s.getAddress());

                        //调用字符缓冲输出流对象的方法写数据
                        bw.write(sb.toString());
                        bw.newLine();
                        bw.flush();
                    }

                    //释放资源
                    bw.close();

//                    return;//或者使用return
                    break outerloop;//注意点1：跳出while循环；或者使用System.exit(0);
                    //循环嵌套：while内套switch，跳出的是switch循环
                default:
                    System.out.println("没有这个选项");
                    break;
            }
        }
    }

    //将内容写入text文档




    public static void add(ArrayList<Student> list){
        //将输入存入学生对象中，将学生对象存入集合中
        Scanner sc = new Scanner(System.in);
        System.out.println("请添加学生姓名:");
        String name = sc.next();
        System.out.println("请添加学生年龄:");
        int age = sc.nextInt();
        System.out.println("请添加学生id:");
        String id = sc.next();

        for (int i = 0; i < list.size(); i++) {
            if(id.equals(list.get(i).getId())){
                System.out.println("您的id重复了");
                return;//结束add方法
            }
        }

        System.out.println("请添加学生地址:");
        String address = sc.next();
        //无参构造
//        //创建一个学生对象
//        Student stu = new Student();
//        //将属性设置到学生对象中
//        stu.setId(id);
//        stu.setName(name);
//        stu.setAge(age);
//        stu.setAddress(address);
        //或者使用有参构造
        Student stu = new Student(name,age,id,address);
        //将学生对象添加到集合中
        list.add(stu);
        //提示
        System.out.println("学生信息添加成功");
    }

    public static void update(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要更新学生的id:");
        String upid = sc.next();
        for (int i = 0; i < list.size(); i++) {
            if(upid.equals(list.get(i).getId())){

                System.out.println("请添加学生姓名:");
                String name = sc.next();
                System.out.println("请添加学生年龄:");
                int age = sc.nextInt();
                System.out.println("请添加学生id:");
                String id = sc.next();
                System.out.println("请添加学生地址:");
                String address = sc.next();

                list.get(i).setId(id);
                list.get(i).setName(name);
                list.get(i).setAge(age);
                list.get(i).setAddress(address);

            }else{
                System.out.println("没有这个学生");
            }
        }
    }

    public static void delete(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("请删除学生的id:");
        String id = sc.next();
        boolean flagHaveStudent = false;//
        for (int i = 0; i < list.size(); i++) {
            if(id.equals(list.get(i).getId())){
                list.remove(i);
                flagHaveStudent = true;
            }
        }
        if(flagHaveStudent){
            System.out.println("删除成功");
        }else{
            System.out.println("没有这个学生");
        }
    }

    public static void query(ArrayList<Student> list){
        if(list.size()==0){
            System.out.println("系统中没有任何学生信息！");
            return;
        }
        System.out.println("姓名\tid\t年龄\t地址");
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);//从集合中取对象叫做stu
            System.out.println(stu.getId()+"\t"+ stu.getName()+"\t"+ stu.getAge()+"\t"+stu.getAddress());//获取对象的每一个属性
        }
    }
}
